/* main.cpp -- starts up gtk and brings up the main window
 * main.cpp is part of Robodrome
 *
 * Copyright (c) 2009 Alex French
 * Robodrome is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Robodrome is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <libglademm/xml.h>
#include <gtkmm.h>
#include <iostream>
#include <audiere.h>
#include "mainwindow.h"
#include "debugger.h"

unsigned int selected_player;
std::vector<Robot> players;
Gtk::Dialog *error_dialog;
Gtk::Label *error_message;
audiere::AudioDevicePtr audio_device;
audiere::SoundEffectPtr collision_snd;
audiere::SoundEffectPtr death_snd;
audiere::SoundEffectPtr gun_snd;
audiere::SoundEffectPtr hellbore_snd;
audiere::SoundEffectPtr hit_snd;
audiere::SoundEffectPtr mine_snd;
audiere::SoundEffectPtr missile_snd;
audiere::SoundEffectPtr nuke_snd;
bool play_snd[8];
bool sounds_enabled;
bool display_enabled;
Debugger *debugger;
int use_debugger;

int main (int argc, char **argv)
{
	try {
		Gtk::Main kit(argc, argv);
		Glib::RefPtr<Gnome::Glade::Xml> refXml = Gnome::Glade::Xml::create("interface.glade");

		selected_player = 0;

		refXml->get_widget("error_dialog", error_dialog);
		if (!error_dialog) throw std::runtime_error("Couldn't find error_dialog in interface.glade");

		refXml->get_widget("error_message", error_message);
		if (!error_message) throw std::runtime_error("Couldn't find error_message in interface.glade");

		refXml->get_widget_derived("debugger", debugger);
		if (!debugger) throw std::runtime_error("Couldn't find debugger in interface.glade");

		// clear sound queue
		for (int i = 0; i < 8; i++)
			play_snd[i] = false;

		// load sounds
		if (!(audio_device = audiere::OpenDevice()))
			throw std::runtime_error("Failed to open sound device.");
		if (!(collision_snd = audiere::OpenSoundEffect(audio_device, "sound/collision.wav", audiere::MULTIPLE, audiere::FF_AUTODETECT)))
			throw std::runtime_error("Failed to open sound/collision.wav");
		if (!(death_snd = audiere::OpenSoundEffect(audio_device, "sound/death.wav", audiere::MULTIPLE, audiere::FF_AUTODETECT)))
			throw std::runtime_error("Failed to open sound/death.wav");
		if (!(gun_snd = audiere::OpenSoundEffect(audio_device, "sound/gun.wav", audiere::MULTIPLE, audiere::FF_AUTODETECT)))
			throw std::runtime_error("Failed to open sound/gun.wav");
		if (!(hellbore_snd = audiere::OpenSoundEffect(audio_device, "sound/hellbore.wav", audiere::MULTIPLE, audiere::FF_AUTODETECT)))
			throw std::runtime_error("Failed to open sound/hellbore.wav");
		if (!(hit_snd = audiere::OpenSoundEffect(audio_device, "sound/hit.wav", audiere::MULTIPLE, audiere::FF_AUTODETECT)))
			throw std::runtime_error("Failed to open sound/jit.wav");
		if (!(mine_snd = audiere::OpenSoundEffect(audio_device, "sound/mine.wav", audiere::MULTIPLE, audiere::FF_AUTODETECT)))
			throw std::runtime_error("Failed to open sound/mine.wav");
		if (!(missile_snd = audiere::OpenSoundEffect(audio_device, "sound/missile.wav", audiere::MULTIPLE, audiere::FF_AUTODETECT)))
			throw std::runtime_error("Failed to open sound/missile.wav");
		if (!(nuke_snd = audiere::OpenSoundEffect(audio_device, "sound/nukebang.wav", audiere::MULTIPLE, audiere::FF_AUTODETECT)))
			throw std::runtime_error("Failed to open sound/nukebang.wav");

		// enable sounds
		sounds_enabled = true;

		// enable display
		display_enabled = true;

		// disabe debugger
		use_debugger = -1;

		MainWindow win(refXml);
		kit.run(win.get_window());
		return 0;
	} catch (std::exception const& ex) {
		std::cerr << "Error: " << ex.what() << std::endl;
		return 1;
	}
}
